import axios from 'axios'
import Vue from 'vue'

export function request(config) {
  const instance = axios.create({
    baseURL: 'http://kumanxuan1.f3322.net:8001',
    timeout: 5000
  })

// 2.axios的拦截器
// 2.1.请求拦截的作用
  instance.interceptors.request.use(config => {
    const token = sessionStorage.getItem('token')
    if(token) {
      config.headers['X-Nideshop-Token'] = token
    }
    Vue.prototype.$toast.loading({
      message: '加载中',
      forbidClick: true,
    })
    return config
  }, err => {
    return Promise.reject(err)
  })

// 2.2.响应拦截
  instance.interceptors.response.use(res => {
    return res.data
  }, err => {
    return Promise.reject(err)
  })

  return instance(config)
}

